package org.hepeng.resiliencedefense;

import org.hepeng.resiliencedefense.config.PropertiesConfigDataGetter;
import org.hepeng.resiliencedefense.config.ResiliencedefenseProperties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author he peng
 */
public class PropertiesFileResiliencedefenseInitializer extends AbstractResiliencedefenseInitializer {

    private Properties configProperties = new Properties();
    private AtomicBoolean isLoadConfigProperties = new AtomicBoolean(false);

    public PropertiesFileResiliencedefenseInitializer(String configPropertiesFilePath) throws IOException {
        readConfigPropertiesFile(configPropertiesFilePath);
    }

    private void readConfigPropertiesFile(String path) throws IOException {
        if (isLoadConfigProperties.compareAndSet(false , true)) {
            InputStream input = ClassLoader.getSystemResourceAsStream(path);
            this.configProperties.load(input);
            ResiliencedefenseProperties resiliencedefenseProperties = new ResiliencedefenseProperties(
                    ResiliencedefenseProperties.DEFAULT_DEPENDENCY_ID ,
                    ResiliencedefenseProperties.DEFAULT_API_ID , configProperties);
            super.configDataGetter = new PropertiesConfigDataGetter(this.configProperties);
            super.configDataGetter.putConfigData(ResiliencedefenseProperties.DEFAULT_DEPENDENCY_ID ,
                    ResiliencedefenseProperties.DEFAULT_API_ID , resiliencedefenseProperties);
        }
    }
}
